Huomasin joidenkin mallien tarvivan erillistä vuosi tai kuukausi saraketta, joten tässä se luodaan.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import plotly.io as pio
pio.renderers.default='notebook'
url_src = "data/LBNL_file.csv"
df = pd.read_csv(url_src, low_memory=False, parse_dates=True)
df_a = df[['state','city','zip_code','customer_segment','total_installed_price','third_party_owned','self_installed',
'system_size_DC','efficiency_module_1','efficiency_module_2','efficiency_module_3','installation_date']]
df_a = df_a.replace(-9999,np.nan)
df_a[['efficiency_module_1','efficiency_module_2','efficiency_module_3']] = df_a[['efficiency_module_1','efficiency_module_2',
'efficiency_module_3']].fillna(0)
df_a['city'] = df_a['city'].str.upper()
# ER arvo (valinnainen)
# Luodaan moduuli laskuri
df_a.loc[df_a['efficiency_module_1'].copy() > 0,'modules'] = 1
df_a.loc[df_a['efficiency_module_2'] > 0,'modules'] += 1
df_a.loc[df_a['efficiency_module_3'] > 0,'modules'] += 1
df_a['ER'] = (df_a['efficiency_module_1']+df_a['efficiency_module_2']+df_a['efficiency_module_3'])/df_a['modules']
print('NaN Module:',len(df_a.loc[df_a['modules'].isna()]))
print('1 Module:',len(df_a.loc[df_a['modules'] == 1]))
print('2 Modules:',len(df_a.loc[df_a['modules'] == 2]))
print('3 Modules:',len(df_a.loc[df_a['modules'] == 3]))
NaN Module: 94297 1 Module: 898202 2 Modules: 6353 3 Modules: 1148
# Jätetään moduliton data pois
df_a['modules'] = df_a['modules'].fillna(0)
df_a = df_a.loc[df_a['modules'] != 0]
df_a = df_a.loc[df_a['total_installed_price'] != 0]
df_a = df_a.loc[df_a['total_installed_price'] < 60000000]
df_a = df_a.loc[df_a['system_size_DC'] >= 1]
df_a = df_a.loc[df_a['self_installed'] == 0]
df_a = df_a.loc[df_a['third_party_owned'] == 0]
# Otetaan vaan Kalifornia
df_a = df_a.loc[df_a['state'] == 'CA']
df_a['installation_date'] = pd.to_datetime(df_a['installation_date'])
df_a = df_a.sort_values(by='installation_date')
# Luodaan vuodet
df_a['year'] = df_a['installation_date'].dt.year
# Luodaan kuukaudet
df_a['month'] = df_a['installation_date'].dt.month
# Testit
print('Years',df_a['year'].unique())
print('Months',df_a['month'].unique())
print('Years',len(df_a['year'].unique()))
print('Months',len(df_a['month'].unique()))
Years [2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019] Months [12 5 6 7 8 9 10 11 1 2 3 4] Years 19 Months 12
print(df_a[['year','month']].describe(datetime_is_numeric=True))
year month count 440082.000000 440082.000000 mean 2016.243768 6.936482 std 2.873702 3.456945 min 2001.000000 1.000000 25% 2015.000000 4.000000 50% 2017.000000 7.000000 75% 2018.000000 10.000000 max 2019.000000 12.000000
# Muutetaan stringeiksi niin saadaan esiintymät
df_a['year'] = df_a['year'].astype(str)
df_a['month'] = df_a['month'].astype(str)
print(df_a[['year','month']].describe(datetime_is_numeric=True))
year month count 440082 440082 unique 19 12 top 2019 12 freq 92781 46958
# Alku vuodet pois
df_a['year'] = df_a['year'].astype(int)
df_a['month'] = df_a['month'].astype(int)
df_a = df_a.loc[df_a['year'] > 2001]
print('Years',df_a['year'].unique())
print('Years',len(df_a['year'].unique()))
Years [2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019] Years 18
Koska vuosia on 18 voidaan data jakaa vielä 2 osaan ainakin testimielessä.
# modules arvo kuvaa moduulien määrää
cols = ['city', 'zip_code', 'customer_segment', 'total_installed_price', 'system_size_DC',
'installation_date', 'modules', 'ER', 'year', 'month']
df_f = df_a[cols].loc[df_a['year'] < 2011]
df_l = df_a[cols].loc[df_a['year'] > 2010]
df_f = df_f.dropna()
df_f.describe(include='all',datetime_is_numeric=True)
| city | zip_code | customer_segment | total_installed_price | system_size_DC | installation_date | modules | ER | year | month | |
|---|---|---|---|---|---|---|---|---|---|---|
| count | 31622 | 31622 | 31622 | 3.162200e+04 | 31622.000000 | 31622 | 31622.000000 | 31622.000000 | 31622.000000 | 31622.000000 |
| unique | 870 | 1271 | 5 | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| top | SAN FRANCISCO | 95472 | RES | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| freq | 1299 | 370 | 30621 | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| mean | NaN | NaN | NaN | 5.620902e+04 | 7.847568 | 2008-12-26 07:47:40.464233728 | 1.021030 | 0.147019 | 2008.473721 | 6.677218 |
| min | NaN | NaN | NaN | 3.481000e+03 | 1.000000 | 2002-05-17 00:00:00 | 1.000000 | 0.054687 | 2002.000000 | 1.000000 |
| 25% | NaN | NaN | NaN | 2.409700e+04 | 3.060000 | 2008-02-15 00:00:00 | 1.000000 | 0.132502 | 2008.000000 | 4.000000 |
| 50% | NaN | NaN | NaN | 3.422053e+04 | 4.600000 | 2009-04-11 00:00:00 | 1.000000 | 0.140773 | 2009.000000 | 7.000000 |
| 75% | NaN | NaN | NaN | 4.877235e+04 | 6.750000 | 2010-03-07 00:00:00 | 1.000000 | 0.168799 | 2010.000000 | 10.000000 |
| max | NaN | NaN | NaN | 1.202354e+07 | 1308.320000 | 2010-12-31 00:00:00 | 3.000000 | 0.195007 | 2010.000000 | 12.000000 |
| std | NaN | NaN | NaN | 2.454496e+05 | 37.194047 | NaN | 0.153701 | 0.021007 | 1.515943 | 3.428471 |
df_l = df_l.dropna()
df_l.describe(include='all',datetime_is_numeric=True)
| city | zip_code | customer_segment | total_installed_price | system_size_DC | installation_date | modules | ER | year | month | |
|---|---|---|---|---|---|---|---|---|---|---|
| count | 408459 | 408459 | 408459 | 4.084590e+05 | 408459.000000 | 408459 | 408459.000000 | 408459.000000 | 408459.000000 | 408459.000000 |
| unique | 1140 | 1556 | 5 | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| top | SAN DIEGO | 95762 | RES | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| freq | 25835 | 3698 | 396049 | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| mean | NaN | NaN | NaN | 3.832358e+04 | 10.109804 | 2017-05-19 12:10:28.974756608 | 1.006693 | 0.184187 | 2016.845346 | 6.956542 |
| min | NaN | NaN | NaN | 1.000000e-02 | 1.000000 | 2011-01-01 00:00:00 | 1.000000 | 0.058182 | 2011.000000 | 1.000000 |
| 25% | NaN | NaN | NaN | 1.745860e+04 | 4.160000 | 2016-04-12 00:00:00 | 1.000000 | 0.169643 | 2016.000000 | 4.000000 |
| 50% | NaN | NaN | NaN | 2.452000e+04 | 5.886000 | 2017-08-17 00:00:00 | 1.000000 | 0.184490 | 2017.000000 | 7.000000 |
| 75% | NaN | NaN | NaN | 3.369600e+04 | 8.125000 | 2018-11-23 00:00:00 | 1.000000 | 0.200157 | 2018.000000 | 10.000000 |
| max | NaN | NaN | NaN | 2.000000e+07 | 4445.280000 | 2019-12-31 00:00:00 | 3.000000 | 0.227273 | 2019.000000 | 12.000000 |
| std | NaN | NaN | NaN | 1.665864e+05 | 45.465750 | NaN | 0.092432 | 0.019408 | 1.919150 | 3.458329 |
print('Asennusten määrän kertautuminen ensimmäiseltä puoliskolta jälkimmäiselle n.',round(df_l['city'].count()/df_f['city'].count()))
print('Max hinnan kertautuminen ensimmäiseltä puoliskolta jälkimmäiselle n.',round(df_l['total_installed_price'].max()/df_f['total_installed_price'].max()))
Asennusten määrän kertautuminen ensimmäiseltä puoliskolta jälkimmäiselle n. 13 Max hinnan kertautuminen ensimmäiseltä puoliskolta jälkimmäiselle n. 2
res_f = df_f.loc[df_f['customer_segment']=='RES']
res_f = res_f.drop(['customer_segment'], axis=1)
res_f.describe(include='all',datetime_is_numeric=True)
| city | zip_code | total_installed_price | system_size_DC | installation_date | modules | ER | year | month | |
|---|---|---|---|---|---|---|---|---|---|
| count | 30621 | 30621 | 3.062100e+04 | 30621.000000 | 30621 | 30621.000000 | 30621.000000 | 30621.000000 | 30621.000000 |
| unique | 857 | 1247 | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| top | SAN FRANCISCO | 95472 | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| freq | 1258 | 357 | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| mean | NaN | NaN | 4.080882e+04 | 5.497711 | 2008-12-21 06:34:41.630253824 | 1.019790 | 0.147171 | 2008.460207 | 6.673427 |
| min | NaN | NaN | 3.481000e+03 | 1.000000 | 2002-05-17 00:00:00 | 1.000000 | 0.054687 | 2002.000000 | 1.000000 |
| 25% | NaN | NaN | 2.389100e+04 | 3.040000 | 2008-02-08 00:00:00 | 1.000000 | 0.132502 | 2008.000000 | 4.000000 |
| 50% | NaN | NaN | 3.365700e+04 | 4.510000 | 2009-04-10 00:00:00 | 1.000000 | 0.140802 | 2009.000000 | 7.000000 |
| 75% | NaN | NaN | 4.733800e+04 | 6.450000 | 2010-03-05 00:00:00 | 1.000000 | 0.168799 | 2010.000000 | 10.000000 |
| max | NaN | NaN | 2.234244e+06 | 361.900000 | 2010-12-31 00:00:00 | 3.000000 | 0.195007 | 2010.000000 | 12.000000 |
| std | NaN | NaN | 3.676544e+04 | 4.898112 | NaN | 0.149023 | 0.020953 | 1.522622 | 3.423477 |
com_f = df_f.loc[df_f['customer_segment']=='COM']
com_f = com_f.drop(['customer_segment'], axis=1)
com_f.describe(include='all',datetime_is_numeric=True)
| city | zip_code | total_installed_price | system_size_DC | installation_date | modules | ER | year | month | |
|---|---|---|---|---|---|---|---|---|---|
| count | 708 | 708 | 7.080000e+02 | 708.000000 | 708 | 708.000000 | 708.000000 | 708.000000 | 708.000000 |
| unique | 273 | 368 | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| top | SAN FRANCISCO | 95448 | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| freq | 28 | 13 | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| mean | NaN | NaN | 4.862635e+05 | 78.515132 | 2009-05-07 19:45:45.762711808 | 1.038136 | 0.141814 | 2008.853107 | 6.484463 |
| min | NaN | NaN | 8.171000e+03 | 1.170000 | 2002-07-05 00:00:00 | 1.000000 | 0.060306 | 2002.000000 | 1.000000 |
| 25% | NaN | NaN | 4.954597e+04 | 6.907500 | 2008-10-11 00:00:00 | 1.000000 | 0.132502 | 2008.000000 | 3.000000 |
| 50% | NaN | NaN | 1.044572e+05 | 14.400000 | 2009-05-18 12:00:00 | 1.000000 | 0.137410 | 2009.000000 | 6.500000 |
| 75% | NaN | NaN | 3.176611e+05 | 49.140000 | 2010-05-08 06:00:00 | 1.000000 | 0.144158 | 2010.000000 | 10.000000 |
| max | NaN | NaN | 9.480240e+06 | 1308.320000 | 2010-12-30 00:00:00 | 2.000000 | 0.195007 | 2010.000000 | 12.000000 |
| std | NaN | NaN | 1.106905e+06 | 187.805001 | NaN | 0.191659 | 0.019655 | 1.296674 | 3.639876 |
res_l = df_l.loc[df_l['customer_segment']=='RES']
res_l = res_l.drop(['customer_segment'], axis=1)
res_l.describe(include='all',datetime_is_numeric=True)
| city | zip_code | total_installed_price | system_size_DC | installation_date | modules | ER | year | month | |
|---|---|---|---|---|---|---|---|---|---|
| count | 396049 | 396049 | 3.960490e+05 | 396049.000000 | 396049 | 396049.000000 | 396049.000000 | 396049.000000 | 396049.000000 |
| unique | 1123 | 1527 | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| top | SAN DIEGO | 95762 | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| freq | 25301 | 3683 | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| mean | NaN | NaN | 2.704604e+04 | 6.495688 | 2017-05-24 07:41:28.646606336 | 1.006062 | 0.184489 | 2016.857649 | 6.967246 |
| min | NaN | NaN | 1.000000e-02 | 1.000000 | 2011-01-01 00:00:00 | 1.000000 | 0.058182 | 2011.000000 | 1.000000 |
| 25% | NaN | NaN | 1.725009e+04 | 4.140000 | 2016-04-13 00:00:00 | 1.000000 | 0.169643 | 2016.000000 | 4.000000 |
| 50% | NaN | NaN | 2.417401e+04 | 5.800000 | 2017-08-21 00:00:00 | 1.000000 | 0.184875 | 2017.000000 | 7.000000 |
| 75% | NaN | NaN | 3.284000e+04 | 7.920000 | 2018-11-27 00:00:00 | 1.000000 | 0.200157 | 2018.000000 | 10.000000 |
| max | NaN | NaN | 3.129000e+06 | 1097.555000 | 2019-12-31 00:00:00 | 3.000000 | 0.227273 | 2019.000000 | 12.000000 |
| std | NaN | NaN | 2.629959e+04 | 6.159360 | NaN | 0.088279 | 0.019311 | 1.908861 | 3.456094 |
com_l = df_l.loc[df_l['customer_segment']=='COM']
com_l = com_l.drop(['customer_segment'], axis=1)
com_l.describe(include='all',datetime_is_numeric=True)
| city | zip_code | total_installed_price | system_size_DC | installation_date | modules | ER | year | month | |
|---|---|---|---|---|---|---|---|---|---|
| count | 9831 | 9831 | 9.831000e+03 | 9831.000000 | 9831 | 9831.000000 | 9831.000000 | 9831.000000 | 9831.000000 |
| unique | 702 | 1070 | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| top | SAN DIEGO | 91387 | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| freq | 506 | 97 | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| mean | NaN | NaN | 3.150462e+05 | 116.087514 | 2017-03-20 11:47:19.792493312 | 1.021666 | 0.175246 | 2016.711423 | 6.584885 |
| min | NaN | NaN | 1.000000e-02 | 1.020000 | 2011-01-01 00:00:00 | 1.000000 | 0.058182 | 2011.000000 | 1.000000 |
| 25% | NaN | NaN | 4.146250e+04 | 12.122795 | 2016-05-06 00:00:00 | 1.000000 | 0.163051 | 2016.000000 | 4.000000 |
| 50% | NaN | NaN | 9.746600e+04 | 29.758410 | 2017-06-28 00:00:00 | 1.000000 | 0.171340 | 2017.000000 | 6.000000 |
| 75% | NaN | NaN | 2.779290e+05 | 97.260000 | 2018-09-10 00:00:00 | 1.000000 | 0.190722 | 2018.000000 | 10.000000 |
| max | NaN | NaN | 2.000000e+07 | 2894.100000 | 2019-12-31 00:00:00 | 3.000000 | 0.226994 | 2019.000000 | 12.000000 |
| std | NaN | NaN | 7.129741e+05 | 226.952745 | NaN | 0.162754 | 0.019243 | 1.993129 | 3.490970 |
import plotly.express as px
sc_f = df_f
fig = px.scatter_3d(df_f, x='total_installed_price', y='system_size_DC', z='month',animation_group="modules",
color='ER', animation_frame='year', opacity=0.5, hover_name='city',width=1000, height=1000,
range_color=[0,0.25],
range_x=[0,df_f['total_installed_price'].max()], range_y=[0,df_f['system_size_DC'].max()],
range_z=[1,12], symbol='modules',size='modules',
title='Data 2002-2011 all customers')
fig.show()
fig = px.scatter_3d(res_f, x='total_installed_price', y='system_size_DC', z='month',animation_group="modules",
color='ER', animation_frame='year', opacity=0.5, hover_name='city',width=1000, height=1000,
range_color=[0,0.25],
range_x=[0,res_f['total_installed_price'].max()], range_y=[0,res_f['system_size_DC'].max()],
range_z=[1,12],symbol='modules',size='modules',
title='Data 2002-2011 RES customers')
fig.show()
fig = px.scatter_3d(com_f, x='total_installed_price', y='system_size_DC', z='month',animation_group="modules",
color='ER', animation_frame='year', opacity=0.5, hover_name='city',width=1000, height=1000,
range_color=[0,0.25],
range_x=[0,com_f['total_installed_price'].max()], range_y=[0,com_f['system_size_DC'].max()],
range_z=[1,12],symbol='modules',size='modules',
title='Data 2002-2011 COM customers')
fig.show()
fig = px.scatter_3d(df_l, x='total_installed_price', y='system_size_DC', z='month',animation_group="modules",
color='ER', animation_frame='year', opacity=0.5, hover_name='city',width=1000, height=1000,
range_color=[0,0.25],
range_x=[0,df_l['total_installed_price'].max()], range_y=[0,df_l['system_size_DC'].max()],
range_z=[1,12],symbol='modules',size='modules',
title='Data 2011-2020 all customers')
fig.show()